/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
* under the License.
****************************************************************/
package reco.db

import static org.mockito.Mockito.*

description "Verifies the access to domains behaves as expected"

scenario "having a domain", {
    given "a domain", {
        domain = new DummyDomain()
    }
    and "a field", {
        field = new DummyField()
        field.name = "field"
        field.domain = DummyDomain
        field.definitionCompleted()
    }
}

// note, if used as expected, this circunstance should not be possible
scenario "access to not defined field using get", {
    when "access to undefined field", {
        fieldValue = domain.get(field)
    }
    then "the returned value should be null", {
        fieldValue.shouldBe null
    }
}

scenario "try to get a value using null", {
    when "use null as field when trying to get a value", {
        getValueWithNullField = { ->
            domain.get(null)
        }
    }
    then "the get operation should fail with a NullPointerException", {
        ensureThrows(NullPointerException) {
            getValueWithNullField()
        }
    }
}

// note, if used as expected, this circunstance should not be possible
scenario "access to not defined field using set", {
    when "access to undefined field", {
        domain.set(field, 2)
    }
    then "the field can be accessed with get", {
        domain.get(field).shouldBe 2
    }
}

scenario "try to set a value using null as field", {
    when "use null as field when trying to get a value", {
        setValueWithNullField = { ->
            domain.set(null, 2)
        }
    }
    then "the get operation should fail with a NullPointerException", {
        ensureThrows(NullPointerException) {
            setValueWithNullField()
        }
    }
}

scenario "recently created domain must be considered new", {
    when "a new domain is consutled if it is new", {
        isNewDomain = domain.isNew()
    }
    then "it should respond yes", {
        isNewDomain.shouldBe true
    }
}